﻿using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using System.Linq;
using System.Net;
using System.Runtime.CompilerServices;
using UnityTemplate.API.LogHelper;
using UnityTemplate.Services.Interface;
using Asp.Versioning;

namespace UnityTemplate.API.Controllers
{
    [ApiController]
    [Route("api/v{version:apiVersion}/[controller]")]    
    [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
    [ApiVersion("1.0")]
    public class BaseApiController : ControllerBase
    {

        protected readonly ILoggerHelper Log;

        protected IUserService UserService { get; set; }

        #region 登录用户信息

        public string UserName { get { return GetLoginUserInfo(); } }

        public string Email { get { return GetLoginUserInfo(); } }

        public string UserId { get { return GetLoginUserInfo(); } }

        private string GetLoginUserInfo([CallerMemberName] string propertyName = null)
        {
            if (User.Claims.Any())
            {
                return User.Claims.Where(x => x.Type == propertyName).FirstOrDefault()?.Value ?? "";
            }

            return "";
        }

        #endregion

        public BaseApiController(IUserService userService, ILoggerHelper logger)
        {
            UserService = userService;
            Log = logger;

        }
    }
}
